//
//  DVWeatherView.m
//  QuickCure
//
//  Created by 松鼠君 on 9/12/16.
//  Copyright © 2016 haoge. All rights reserved.
//

#import "DVWeatherView.h"
#import "MaSonry.h"
#import "DVWeatherDataModel.h"
#import "DVWeatherDataDetailData.h"
#import "UIButton+WebCache.h"

#define IS_IPHONE5 (([[UIScreen mainScreen] bounds].size.height == 568) ? YES :NO)

@interface DVWeatherView()
@property(nonatomic,weak)UIButton* showBT;
@property(nonatomic,weak)UILabel* info;
@property(nonatomic,weak)UIView* midLine;
@property(nonatomic,strong)DVWeatherDataModel* model;
@end
@implementation DVWeatherView


- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
        
        [self initializeForView];
    }
    return self;
}

    
-(void)setCityName:(NSString *)cityName{
    
    _cityName = cityName;

  
    if (self.locationBT&&self.info) {
    
#pragma mark
#pragma mark "数据延迟Bug "
        
// DVWeatherDataDetailData* DVWeatherDataDetailData = self.model.weather_data.firstObject ;

        
        [self.locationBT setTitle:self.cityName forState: UIControlStateNormal];
        
        self.locationBT.titleLabel.attributedText = [[NSAttributedString alloc]initWithString:self.locationBT.titleLabel.text attributes:@{NSUnderlineStyleAttributeName:@1}];
        
//        NSLog(@"self.model = %@",self.model);
        
        [self.model fecthDataWithCityName:cityName andBlock:^(BOOL dayType) {
            
        DVWeatherDataDetailData* DVWeatherDataDetailData = self.model.weather_data.firstObject ;
            
            
            self.info.text = [NSString stringWithFormat:@"温度:%@\n日期:%@",DVWeatherDataDetailData.temperature,DVWeatherDataDetailData.date];
            
            [self.showBT setTitle:[NSString stringWithFormat:@"%@",DVWeatherDataDetailData.weather] forState: UIControlStateNormal];
            
            
            dayType? [self.showBT sd_setImageWithURL:DVWeatherDataDetailData.dayPictureUrl forState:UIControlStateNormal]: [self.showBT sd_setImageWithURL:DVWeatherDataDetailData.nightPictureUrl forState:UIControlStateNormal];
        


            
            
        }];
        
        
        

        
        //这个重新计算 BT 大小使文字和 BT 大小一致
        
        
//        [self.showBT sizeToFit];
//        
//        [self.locationBT sizeToFit];

    }
    
    
    
}
    


-(void)initializeForView{
    

    self.cityName = @"上海";
    
    self.backgroundColor = [UIColor colorWithRed:0.5 green:0.9 blue:0.8 alpha:0.7];
    
    
    
    
    
    
    

    
    /**
     *  分界线
     *
     */
    
    UIView*  midLine= [UIView new];
    
    self.midLine = midLine;
    
    midLine.backgroundColor = [UIColor grayColor];
    
    [self addSubview:midLine];
    
    [midLine mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.equalTo(self);
        
        make.size.mas_equalTo(CGSizeMake(2, self.bounds.size.height*0.8));
        

    
    }];
    
    
    
    /**
     *  描述信息
     */
    
    
    UILabel* info = [UILabel  new];
    
    self.info = info;

    
    info.text = [NSString stringWithFormat:@"温度:%@\n日期:%@",self.model.weather_data.firstObject.temperature,self.model.weather_data.firstObject.date];
    
    info.textColor = [UIColor colorWithRed:0.1 green:0.5 blue:0.4 alpha:1];
    
    
    // UIlabel 内部换行
    
    //判断机型
    
    
    if (IS_IPHONE5) {
        
        [self adjustStringLineSpacingWithLable:info andSpacing:5];
        
//        NSLog(@"%s",__func__);

        
    }else{
        
        [self adjustStringLineSpacingWithLable:info andSpacing:10];

    }
    
    
    info.font = [UIFont systemFontOfSize:16];
    
    info.numberOfLines = 0;
   
    info.textAlignment = NSTextAlignmentCenter;
    
    [self addSubview:info];

    [info mas_makeConstraints:^(MASConstraintMaker *make) {
        
        CGFloat padding = 10.0f;
        
        make.centerY.mas_equalTo(self);
        
        make.left.equalTo(midLine.mas_right).offset(padding);
        
        make.right.equalTo(self).offset(-padding);
        
        // make.width.lessThanOrEqualTo(@(self.bounds.size.width*(2/5)));
        
        
    }];
    
    
    
    // 左边
    
    UIButton* showBT = [UIButton new];
    
    
    [showBT setImage:[UIImage imageNamed:@"yin"] forState:UIControlStateNormal];
    
    showBT.titleEdgeInsets =  UIEdgeInsetsMake(0, 5, 0, -5);
    
    self.showBT = showBT;
    
    
    [showBT setTitle:@"未知天气" forState: UIControlStateNormal];
    
    showBT.titleLabel.numberOfLines = 0;
    
        [self.showBT setTitleColor: [UIColor colorWithRed:0.1 green:0.5 blue:0.4 alpha:1] forState:UIControlStateNormal];
    
    showBT.titleLabel.textAlignment = NSTextAlignmentCenter;
    
     showBT.titleLabel.font = [UIFont systemFontOfSize:14];
    
    [self addSubview:showBT];
    

    [showBT mas_makeConstraints:^(MASConstraintMaker *make) {
        
                CGFloat padding = 10.0f;
        
        make.left.equalTo(self).offset(padding);
        
        make.centerY.equalTo(self.mas_centerY);
        

        
    }];
    
    showBT.userInteractionEnabled = NO;
    
    
    
    UIButton* locationBT = [UIButton new];
    
    self.locationBT = locationBT;
    
    [self.locationBT setTitleColor: [UIColor colorWithRed:0.1 green:0.5 blue:0.4 alpha:1] forState:UIControlStateNormal];

    
    //    locationBT.titleLabel.textAlignment = NSTextAlignmentCenter;
    
      self.locationBT.titleLabel.font = [UIFont systemFontOfSize:15];
    
    [self.locationBT setTitle:self.cityName forState: UIControlStateNormal];
    
    self.locationBT.titleLabel.numberOfLines = 0;

    
      self.locationBT.titleLabel.attributedText = [[NSAttributedString alloc]initWithString:self.locationBT.titleLabel.text attributes:@{NSUnderlineStyleAttributeName:@1}];
    
    [self addSubview:  self.locationBT];
    

    
    
    
    
    


    [locationBT mas_makeConstraints:^(MASConstraintMaker *make) {
        
        // 自适应宽度
        
        CGFloat padding = 5.0f;
        
        make.left.equalTo(showBT.right).offset(padding);
        
        make.right.equalTo(midLine.mas_left).offset(-padding);
        
        make.centerY.equalTo(self.mas_centerY);
        
            make.size.with.lessThanOrEqualTo(60);
        
        
    }];
    

    
    
    self.model  = [[DVWeatherDataModel alloc]initWithCityName:self.cityName andBlock:^(BOOL dayType){
        
        info.text = [NSString stringWithFormat:@"温度:%@\n日期:%@",self.model.weather_data.firstObject.temperature,self.model.weather_data.firstObject.date];
       
        [showBT setTitle:[NSString stringWithFormat:@"%@",self.model.weather_data.firstObject.weather] forState: UIControlStateNormal];
        

        
        if (dayType) {
         
            [showBT sd_setImageWithURL:self.model.weather_data.firstObject.dayPictureUrl forState:UIControlStateNormal];
            
        }else{
            
           [showBT sd_setImageWithURL:self.model.weather_data.firstObject.nightPictureUrl forState:UIControlStateNormal];

        }

        
    }];
    
    
    
    
    
    
}




#pragma mark
#pragma mark "调整行间距"


-(void)adjustStringLineSpacingWithLable:(UILabel*)label andSpacing:(CGFloat)Space{
    
    
    NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:label.text];
    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
    
    [paragraphStyle setLineSpacing:Space];//调整行间距
    
    [attributedString addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, [label.text length])];
    
    label.attributedText = attributedString;
    
    [label sizeToFit];
    
    
}

@end
